﻿<%@ Page Title="" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true"
    EnableEventValidation="false" Inherits="ReservationList" Codebehind="ReservationList.aspx.cs" %>

<asp:Content ID="Content1" ContentPlaceHolderID="using" runat="server">

    <script type="text/javascript" src="/js/jquery-ui.min.js"></script>

    <script type="text/javascript" src="/js/jquery-jtemplates.js"></script>

    <script src="/Pages/Reservations/Js/reservationlist.js" type="text/javascript"></script>

    <script type="text/javascript" src="/js/smartpaginator.js"></script>

    <link href="/css/smartpaginator.css" rel="stylesheet" type="text/css" />

    <script type="text/html" id="TemplateResultsTable">    
    {#template MAIN}
    <table id="gridReservation" cellpadding="5" cellspacing="0" class="grid">
      <thead>
        <th>Seq</th>
        <th>Departure date</th>
        <th># of Pax</th>
        <th>Route</th>
        <th>Booking code</th>
        <th>Agent #</th>
        <th>Email</th>
        <th>Client type</th>
        <th>Status</th>
        <th><a href="/Pages/Reservations/Reservation.aspx" class="btnNew">Book</a></th>
      </thead>
      <tbody>
      {#foreach $T.List as reservation}
      <tr class="{#cycle values=['','evenRow']}">
      <td align="center">{$T.reservation$index + 1}</td>
      {#include ROW root=$T.reservation}
      <td>
        <a href='ReservationEdit.aspx?ReservationID={$T.reservation.ReservationID}' class="btnEdit">Edit</a>
        <%if (HttpContext.Current.User.IsInRole("Administrator"))
        { %>
        / <a reservationid="{$T.reservation.ReservationID}" style={#if $T.reservation.DeleteHidden} "display: inline" {#else} "display: none" {#/if} class="btnDelete">Delete</a>
        <%} %></td>
    </tr>
      {#/for}
      </tbody>
    </table>
    {#/template MAIN}

    {#template ROW}     
      <td>{formatJSONDate($T.CheckInDate)}</td>
      <td>{$T.NumOfPax}</td>
      <td>{$T.RouteNumber}{#if $T.SubRouteCode != null && $T.SubRouteCode != '' }-{$T.SubRouteCode}{#/if}</td>
      <td>{$T.ReservationCode}</td>
      <td>{$T.GuestID}</td>
      <td>{$T.Email}</td>
      <td>{formatClientType($T.GuestType)}</td>
      <td>{formatStatus($T.Status)}</td>
    {#/template ROW}
    
    </script>

</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="content" runat="server">
    <div style="margin: 10px 3px 0 3px">
        <!-- Title -->
        <h1 style="text-indent: 10px">
            Search Bookings</h1>
        <table style="margin: 15px;" class="field">
            <tr>
                <%-- Booking code --%>
                <td>
                    Booking code
                </td>
                <td>
                    <asp:TextBox ID="txtBookingCode" runat="server"></asp:TextBox>
                </td>
                <%-- Status --%>
                <td>
                    Status
                </td>
                <td>
                    <asp:DropDownList ID="ddlStatus" runat="server">
                        <asp:ListItem Value="-1" class="firstItem">All</asp:ListItem>
                        <asp:ListItem Value="-2">Not in progress</asp:ListItem>
                        <asp:ListItem Value="0">In progress</asp:ListItem>
                        <asp:ListItem Value="1">Cancelled</asp:ListItem>
                    </asp:DropDownList>
                </td>
                <%-- Client type --%>
                <td>
                    Client Type
                </td>
                <td>
                    <asp:DropDownList ID="ddlClientType" runat="server">
                        <asp:ListItem Value="-1" class="firstItem">All</asp:ListItem>
                        <asp:ListItem Value="0">Agent</asp:ListItem>
                        <asp:ListItem Value="1">WI</asp:ListItem>
                    </asp:DropDownList>
                </td>
            </tr>
            <tr>
                <%-- Boat --%>
                <td>
                    Boat
                </td>
                <td>
                    <asp:DropDownList ID="ddlBoat" runat="server" AppendDataBoundItems="true">
                        <asp:ListItem Value="" class="firstItem">All</asp:ListItem>
                    </asp:DropDownList>
                </td>
                <%-- Route --%>
                <td>
                    Route
                </td>
                <td>
                    <asp:DropDownList ID="ddlRoute" runat="server" AppendDataBoundItems="true">
                        <asp:ListItem Value="-1" class="firstItem">All</asp:ListItem>
                    </asp:DropDownList>
                </td>
                <%-- Sub Route --%>
                <td>
                    Sub route
                </td>
                <td>
                    <asp:DropDownList ID="ddlSubRoute" runat="server" AppendDataBoundItems="true">
                        <asp:ListItem Value=" " class="firstItem">All</asp:ListItem>
                    </asp:DropDownList>
                </td>
            </tr>
            <tr>
                <%-- From depature --%>
                <td>
                    Board date from
                </td>
                <td>
                    <asp:TextBox ID="txtFromDepDate" runat="server" CssClass="datepicker"></asp:TextBox>
                </td>
                <%-- To depature --%>
                <td>
                    to
                </td>
                <td>
                    <asp:TextBox ID="txtToDepDate" runat="server" CssClass="datepicker"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <%-- From created --%>
                <td>
                    Created date from
                </td>
                <td>
                    <asp:TextBox ID="txtFromCreatedDate" runat="server" CssClass="datepicker"></asp:TextBox>
                </td>
                <%-- To created --%>
                <td>
                    to
                </td>
                <td>
                    <asp:TextBox ID="txtToCreatedDate" runat="server" CssClass="datepicker"></asp:TextBox>
                </td>
                <%-- Button Find --%>
                <td colspan="6">
                    <div class="margin: 0 auto 0 auto; width: auto;">
                        <a style="outline: none;" id="btnFind" title="Search button" class="button"><span>Search</span></a>
                        <%--<a style="outline: none;" id="btnExcel" title="Excel button" class="button"><span>Excel</span></a>--%>
                    </div>
                </td>
            </tr>
        </table>
        <div id="reservation">
        </div>
        <div id="red" style="margin: auto;">
        </div>
    </div>
    <%-- Results Table Template --%>

    <script type="text/javascript">

        var pageSize = 20;

        $(document).ready(function () {

            $("#btnFind").click(function () {
                getData(1, true);
            });

            $("#btnExcel").click(function () {
                getData(1, true);
            });

            $(".field").keydown(function (event) {
                if (event.keyCode == '13') {
                    getData(1, true);
                    return false;
                }
            });

            getData(1, true);
        });

        function getData(pageNumber, pagelink) {
            var bookingCode = $("#<%=txtBookingCode.ClientID %>").val();
            var boatID = $("#<%=ddlBoat.ClientID %> option:selected").val();
            var clientType = $("#<%=ddlClientType.ClientID %> option:selected").val();
            var status = $("#<%=ddlStatus.ClientID %> option:selected").val();
            var routeNumber = parseInt($("#<%=ddlRoute.ClientID %> option:selected").val());
            var subRouteCode = $("#<%=ddlSubRoute.ClientID %> option:selected").val();
            var fromDepartureDate = $("#<%=txtFromDepDate.ClientID %>").val();
            var toDepartureDate = $("#<%=txtToDepDate.ClientID %>").val();
            var fromCreatedDate = $("#<%=txtFromCreatedDate.ClientID %>").val();
            var toCreatedDate = $("#<%=txtToCreatedDate.ClientID %>").val();

            defaultParameters = "{bookingCode:'" + bookingCode + "', boatID: '" + boatID + "', status:" + status + ", clientType: " + clientType + ", routeNumber: " + routeNumber + ", subRouteCode: '" + subRouteCode + "', fromDepartureDate: '" + fromDepartureDate + "', toDepartureDate: '" + toDepartureDate + "', fromCreatedDate: '" + fromCreatedDate + "', toCreatedDate: '" + toCreatedDate + "', pageSize:" + pageSize + ", pageNumber:" + pageNumber + "}";
            //    Loader(true);
            loading($('#reservation'));

            $.ajax({
                type: "POST",
                url: "/Get.asmx/GetReservationList",
                data: defaultParameters,
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: (function Success(data, status) {
                    var list = data.d;
                    $('#reservation').setTemplate($("#TemplateResultsTable").html());
                    $('#reservation').processTemplate(list);

                    if (pagelink != null) {
                        $('#red').smartpaginator({
                            totalrecords: list.TotalRecords,
                            recordsperpage: list.PageSize,
                            length: 8,
                            next: 'Next',
                            prev: 'Prev',
                            first: 'First',
                            last: 'Last',
                            theme: 'red',
                            controlsalways: true,
                            onchange: function (newPage) {
                                getData(newPage);
                                $('#r').html('Page # ' + newPage);
                            }
                        });
                    }

                    // setPageNumber();

                    $("#gridReservation a.btnNew").button();

                    $("#gridReservation a.btnDelete").click(function () {

                        var result = confirm("Are you sure you want to delete this record?");
                        if (result) {
                            var aTag = $(this);

                            // load provider
                            $.ajax({
                                type: "POST",
                                url: "/Update.asmx/DeleteReservation",
                                data: "{ reservationID:'" + aTag.attr("reservationid") + "'}",
                                contentType: "application/json; charset=utf-8",
                                dataType: "json",
                                async: false,
                                success: (function Success(data, status) {
                                    if (data.d) {
                                        // Select provider
                                        $('#gridReservation > tbody > tr').removeClass('selected');
                                        aTag.parents("tr:first").remove();
                                        return false;
                                    }
                                    else {

                                    }
                                }),
                                error: (function Error(request, status, error) {
                                    $("#reservation").html(request.statusText).fadeIn(1000);
                                })
                            });
                        }
                    });
                }),
                error: (function Error(request, status, error) {
                    $("#reservation").html(request.statusText).fadeIn(1000);
                })
            });
        }

        //        function setPageNumber() {
        //            $(".pagenumber").click(function () {
        //                $(".pagenumber").removeClass("actived");
        //                $(this).addClass("actived");

        //                var pageIndex = $(this).val();
        //                if (pageIndex != -1)
        //                    getData(pageIndex);
        //            });
        //        }

        function formatClientType(integer) {
            return integer == 0 ? "Agent" : "WI";
        }

        function formatStatus(integer) {
            return integer == 0 ? "Inprogress" : (integer == 1 ? "Cancelled" : "Unknown");
        }

        function loading(tag) {
            tag.empty().html('<center><img src="/Images/ajax-loader.gif" style="margin-top: 168px" /><center>');
        }
        
    </script>

</asp:Content>
